#include "stm32f4xx.h"                  // Device header
#include "IC.h"
int cnt;
int set_back;

void IC_Init(void)
{
	 // reused F6 as pwm capture (TIM2_CH1), TIM2_CH1
	TIM_TimeBaseInitTypeDef TIM_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOF, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource6,GPIO_AF_TIM10);
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10,ENABLE);
	TIM_InternalClockConfig(TIM10);
	TIM_TimeBaseStructInit(&TIM_InitStruct);
	TIM_InitStruct.TIM_Prescaler = 168-1; // 1Mhz
	TIM_TimeBaseInit(TIM10,&TIM_InitStruct);
	TIM_ITConfig(TIM10, TIM_IT_CC1, ENABLE);
	
	TIM_ICInitTypeDef TIM_ICStruct;
	TIM_ICStructInit(&TIM_ICStruct);
	TIM_ICInit(TIM10,&TIM_ICStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitStruct);
	
	

	TIM_Cmd(TIM10,ENABLE);
}
/*
void TIM3_IRQHandler(void){

}*/
//extern int freq;
//void TIM1_UP_TIM10_IRQHandler(void)
//{
//	if(TIM_GetITStatus(TIM10,TIM_FLAG_CC1)!=RESET){
//		int v1 = TIM_GetCapture1(TIM10);
//		freq = 1000000/(v1+1);
//		if(!set_back) set_back=1;
//		TIM_SetCounter(TIM10,0);
//		//TIM_Cmd(TIM10,ENABLE);
//		
//		TIM_ClearITPendingBit(TIM10,TIM_FLAG_CC1);
//	}
//}

